package org.itboys.mobile.controller.mobile;

import com.google.common.collect.Maps;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.kitchen.conmon.Finals;
import org.itboys.kitchen.util.MobileSignUtils;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.SundayCommentDto;
import org.itboys.mobile.dto.order.KitchenOrderDto;
import org.itboys.mobile.entity.mongo.SundayComment;
import org.itboys.mobile.service.mongo.mobile.KitchenMobileCommonService;
import org.itboys.mobile.service.mongo.mobile.SundayMobileCommentService;
import org.itboys.mobile.service.mongo.mobile.SundayMobileMemberService;
import org.itboys.mobile.service.mongo.web.SundayMemberService;
import org.itboys.mobile.service.mysql.mobile.score.SundayMobileScoreService;
import org.itboys.mobile.service.mysql.web.score.SundayScoreConfigService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import sun.security.provider.Sun;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017年5月9日
 * 描述：花果山（农厨）_移动端_项目自己公用_controller
 */
@Controller
@RequestMapping("/kitchen/mobile/common")
public class SundayMobileCommonController extends BaseController{
    @Autowired
    private KitchenMobileCommonService commonService;
    @Autowired
    private SundayMobileScoreService scoreService;
    @Autowired
    private SundayMobileCommentService commentService;
    @Autowired
    private SundayMobileMemberService memberService;
    @Autowired
    private SundayScoreConfigService configService;



    /**
     * 获取首页所有数据
     * @param request
     * @param response
     * @param villageId
     * @param nonce
     * @param sign
     */
    @RequestMapping(value="/getIndexData", method=RequestMethod.POST)
    @ApiOperation(value="【用户版和管理版】获取首页所有数据。列表内容不分页")
    public void getIndexData(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required=false, name="villageId", value="小区ID（用户版传入）")@RequestParam(value = "villageId", required = false)Long villageId,
            @ApiParam(required = true,value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
            @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){
        try{
            Long memberId = AccountIdHoldUtils.getMemberId();
            Map<String,Object> result =commonService.getIndexData(memberId,villageId);
            ResponseMessage.success(result);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/kitchen/mobile/common/getIndexData----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    @RequestMapping(value="/sign", method= RequestMethod.POST)
    @ApiOperation(value="签到")
    public void sign(HttpServletRequest request,
                     HttpServletResponse response,
                     @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                     @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){
        try{
            Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
            param.put("nonce", nonce);
            param.put("sign", sign);
          /*  boolean signSer = MobileSignUtils.signCheck(param, sign);
            if(!signSer){
                logger.warn("签名校验失败");
                ResponseMessage.error(-1,"签名校验失败");
                return;
            }*/
            Long memberId = AccountIdHoldUtils.getMemberId();
          //  configService
            memberService.changeScore(null,configService.findOne(null).getSignScore(),
                    SundayCommonConstants.SCORE_TYPE_SIGN,memberId);
           // memberService.c


           // scoreService.generateRecord(null,null, SundayCommonConstants.SCORE_TYPE_SIGN,memberId);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/kitchen/mobile/common/sign----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    /**
     *获取积分页面需要的整页数据
     * @param request
     * @param response
     * @param pageSize
     * @param pageNumber
     * @param sort
     * @param order
     * @param nonce
     * @param sign
     */
    @RequestMapping(value="/getScoreData", method= RequestMethod.POST)
    @ApiOperation(value="[用户版]获取积分页面需要的整页数据。")
    public  void getScoreData(HttpServletRequest request,
                              HttpServletResponse response,
                              @ApiParam(required = true, name = "pageSize", value = "分页参数。每页数量。默认20") @RequestParam(value = "pageSize", required = true) String pageSize,
                              @ApiParam(required = true, name = "pageNumber", value = "分页参数。第几页。默认1") @RequestParam(value = "pageNumber", required = true) String pageNumber,
                              @ApiParam(required = false, name = "sort", value = "排序参数。字段名称。默认为空") @RequestParam(value = "sort", required = false) String sort,
                              @ApiParam(required = false, name = "order", value = "排序参数。。默认为空。ASC正序/DESC倒序") @RequestParam(value = "order", required = false) String order,
                              @ApiParam(required = true , name = "type", value = "1签到页，2,我的积分页(含积分记录，当pageNumber>1时，不会请求积分页其他数据)") @RequestParam(value = "type", required = true) Integer type,
                              @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                              @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){
        try{
            Long memberId = AccountIdHoldUtils.getMemberId();
            Map<String,Object> result=scoreService.selectMobileScore(request,memberId,type);
            ResponseMessage.success(result);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/kitchen/mobile/common/getScoreData----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    @RequestMapping(value="/getComment",method= RequestMethod.POST)
    @ApiOperation(value="【用户版和管理版】分页获取评论。")
    public void select(HttpServletRequest request,
                       HttpServletResponse response,
                       @ApiParam(required = true, name = "type", value = "评论类型。1,商品，2，资讯，3，菜谱") @RequestParam(value = "type", required = true)Integer type,
                       @ApiParam(required = true, name = "outClassId", value = "评论主体ID。如要获取商品的评论，传入商品ID") @RequestParam(value = "outClassId", required = true)Long outClassId,

                       @ApiParam(required = true, name = "pageSize", value = "分页参数。每页数量。默认20") @RequestParam(value = "pageSize", required = true) String pageSize,
                       @ApiParam(required = true, name = "pageNumber", value = "分页参数。第几页。默认1") @RequestParam(value = "pageNumber", required = true) String pageNumber,
                       @ApiParam(required = false, name = "sort", value = "排序参数。字段名称。默认为空") @RequestParam(value = "sort", required = false) String sort,
                       @ApiParam(required = false, name = "order", value = "排序参数。。默认为空。ASC正序/DESC倒序") @RequestParam(value = "order", required = false) String order,
                       @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                       @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign
    ) {
        try{
            Long memberId = AccountIdHoldUtils.getMemberId();
            List<SundayCommentDto> result = commentService.getCommentByPage(request,memberId,type,outClassId);
            ResponseMessage.success(result);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/kitchen/mobile/common/getComment----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
}
